<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    width="65%"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="中文名称" prop="name"
                :rules="[
                  {required: true, message:'模板中文名称不能为空', trigger:'blur'},
                  { min: 2, message: '最小长度是2个字符', trigger: 'blur' },
                  {validator: validator.stringCheck, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写模板中文名称"  maxlength="32"  minlength="2"   ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="英文名称" prop="enname"
                :rules="[
                  {required: true, message:'英文名称不能为空', trigger:'blur'},
                  { min: 2, message: '最小长度是2个字符', trigger: 'blur' }
                 ]">
              <el-input v-model="inputForm.enname" placeholder="请填写英文名称"  maxlength="64"  minlength="2"   ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="模板类型" prop="type"
                :rules="[
                  {required: true, message:'模板类型不能为空', trigger:'blur'}
                 ]">
              <el-select v-model="inputForm.type" placeholder="请选择模板类型">
                <el-option
                  v-for="item in templateTypeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
           </el-form-item>
        </el-col>
        <el-col>
          <el-tag type="info">模板内容</el-tag>
          <codemirror
            ref="mycode"
            v-model="inputForm.content"
            :options="cmOptions"
            class="code">
          </codemirror>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
import { codemirror } from 'vue-codemirror'
import 'codemirror/theme/ambiance.css'
require('codemirror/mode/javascript/javascript')
require('codemirror/mode/css/css')
require('codemirror/mode/htmlmixed/htmlmixed')

export default {
  data () {
    return {
      title: '',
      method: '',
      visible: false,
      loading: false,
      inputForm: {
        id: '',
        name: '',
        enname: '',
        type: '',
        content: ''
      },
      templateTypeList: [{id: 'CSS', name: 'CSS'}, {id: 'JS', name: 'JS'}, {id: 'HTML', name: 'HTML'}],
      cmOptions: {
        theme: 'ambiance',
        tabSize: 4,
        // mode: 'text/javascript',
        // mode: 'text/html',
        mode: 'text/x-gss',
        value: '',
        matchBrackets: true, // 括号匹配
        readOnly: false,
        lineNumbers: true,
        extraKeys: { Ctrl: 'autocomplete' }
      }
    }
  },
  components: {
    codemirror
  },
  created () {

  },
  watch: {
    'inputForm.type': {
      handler (newVal, oldVal) {
        if (!newVal) {
          return
        }

        // 设置编辑器模式
        let mode = ''
        switch (newVal) {
          case 'JS':
            mode = 'text/javascript'
            break
          case 'html':
            mode = 'text/html'
            break
          case 'CSS':
            mode = 'text/x-gss'
            break
          default:
            mode = 'text/html'
            break
        }
        this.cmOptions.mode = mode
      },
      immediate: true,
      deep: false
    },
    visible: {
      handler (newVal, oldVal) {
        this.cmOptions.readOnly = !newVal
      },
      immediate: true,
      deep: false
    }
  },
  methods: {
    init (method, id) {
      this.method = method
      this.inputForm.id = id
      if (method === 'add') {
        this.title = `新建模板`
      } else if (method === 'edit') {
        this.title = '修改模板'
      } else if (method === 'view') {
        this.title = '查看模板'
      }
      this.visible = true
      this.loading = false
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields()
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.loading = true
          this.$http({
            url: `/cms/template/cmsTemplate/queryById?id=${this.inputForm.id}`,
            method: 'get'
          }).then(({data}) => {
            this.inputForm = this.recover(this.inputForm, data.cmsTemplate)
            this.loading = false
          })
        }
      })
    },
      // 表单提交
    doSubmit () {
      this.$refs['inputForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http({
            url: `/cms/template/cmsTemplate/save`,
            method: 'post',
            data: this.inputForm
          }).then(({data}) => {
            this.loading = false
            if (data && data.success) {
              this.visible = false
              this.$message.success(data.msg)
              this.$emit('refreshDataList')
            }
          })
        }
      })
    }
  }
}
</script>
